package com.siao.data
{
	import com.siao.events.DataChangeEvent;
	
	import flash.events.EventDispatcher;
	
	[Event(name="change", type="com.siao.events.DataChangeEvent")]
	
	/**
	 * 值对象
	 * @author SiaoLeon
	 */
	public class ValueObject extends EventDispatcher
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		/**
		 * 字典
		 * @default
		 */
		protected var dict:HashMap;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function ValueObject()
		{
			super();
			
			this.dict = new HashMap();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function toString():String
		{
			var result:Array = new Array();
			
			for each (var key:* in this.dict.keys)
			{
				result.push(key + "=" + this.dict.getValue(key));
			}
			
			return result.join(",");
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 获取属性
		 * @param name 属性名称
		 * @param value 默认值
		 */
		protected function getProperty(name:String, value:Object=null):*
		{
			if (!this.dict.containsKey(name))
			{
				this.dict.put(name, value);
			}
			
			return this.dict.getValue(name);
		}
		
		/**
		 * 设置属性
		 * @param name 属性名称
		 * @param value 属性值
		 * @return
		 */
		protected function setProperty(name:String, value:Object):Boolean
		{
			var valueOld:Object = this.dict.getValue(name);
			var isChange:Boolean = this.dict.put(name, value);
			
			if (isChange)
			{
				this.dispatchEvent(new DataChangeEvent(name, valueOld, value));
			}
			
			return isChange;
		}
	
	}
}
